// pages/index/reservation/index.js
import Common from '../../../utils/common.js'
let http = new Common()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    sortList: [],
    sortIndex: '0',
    list: '',
    tabIndex: '0',
    tabs: [
      { name: '优惠预定', img1: '../../../utils/img/005.png', img2: '../../../utils/img/028.png' },
      { name: '计时预定', img1: '../../../utils/img/029.png', img2: '../../../utils/img/006.png' }
    ],
    dateIndex: '0',
    date: '',
    bottomList: [],
    Room: '',
    RoomDate: {
      money: '', date: ''
    },
    DiscountShow: false,
    z_index: false,
    bottom_date: [],
    types: '',
    Bom_date_Details: [],
    aaIndex: '0',
    bbIndex: '2',
    count: {
      count1: false,
      count2: false,
      start: '',
      end: '',
      start2: '',
      end2: '',
      money: ''
    },
    nextIndex: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  date: function () {
    var date = new Date();

    let oldyear = date.getFullYear();
    let oldmonth = wx.getStorageSync('month')
    let oldday = wx.getStorageSync('Day')
    
    let tomonth = date.getMonth()
    let today = date.getDate()

    let m = oldmonth - (tomonth + 1)
    let d = oldday - today

    var days = new Date(oldyear, oldmonth, 0).getDate();

    let aa = ["日", "一", "二", "三", "四", "五", "六",]

    let lists = []
    let todays = new Date(oldyear, tomonth + 1, 0).getDate();
    if (m > 0) {

      for (let i = oldday; i > 0; i--) {
        let day = new Date(oldyear, oldmonth - 1, i)
        let getdate = parseFloat(day.getDate()) < 10 ? '0' + day.getDate() : day.getDate()

        lists.unshift({ week: `星期${aa[day.getDay()]}`, day: `${(day.getMonth() + 1)}.${getdate}`, date: day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getdate })
      }

      for (let i = todays; i > today - 1; i--) {
        let day = new Date(oldyear, tomonth, i)
        let getdate2 = day.getDate() < 10 ? '0' + day.getDate() : day.getDate()
        lists.unshift({ week: `星期${aa[day.getDay()]}`, day: `${(day.getMonth() + 1)}.${getdate2}`, date: day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getdate2 })
      }

    } else {
      
      if (d > 0) {
        for (let i = oldday; i > today - 1; i--) {
          let day = new Date(oldyear, oldmonth - 1, i)
          let getdate = parseFloat(day.getDate()) < 10 ? '0' + day.getDate() : day.getDate()

          lists.unshift({ week: `星期${aa[day.getDay()]}`, day: `${(day.getMonth() + 1)}.${getdate}`, date: day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getdate })
        }
        // console.log(lists)
        // console.log(today)
        // console.log(oldday)
      } else {
        console.log('当天')

        let day = new Date(oldyear, oldmonth - 1, today)
        let getdate = parseFloat(day.getDate()) < 10 ? '0' + day.getDate() : day.getDate()

        lists.unshift({ week: `星期${aa[day.getDay()]}`, day: `${(day.getMonth() + 1)}.${getdate}`, date: day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getdate })
      }
    }
    
    if (m > 0) {
      if ((today - 1) > oldday){
        let we = ''
        if ((today - 1)<=8){
          we = (today - 1)
        }else{
          we = 8
        }
        for (let i = 1; i < we; i++) {
        let day = new Date(oldyear, oldmonth - 1, oldday)
      
          day.setDate(day.getDate() + i)
          let getdate = parseFloat(day.getDate()) < 10 ? '0' + day.getDate() : day.getDate()
          lists.push({ week: `星期${aa[day.getDay()]}`, day: `${(day.getMonth() + 1)}.${getdate}`, date: day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate() })
        }
      }
    } else {
      for (let i = 1; i < 8; i++) {
        let day = new Date(oldyear, oldmonth - 1, oldday)
        
        day.setDate(day.getDate() + i)
        
        let getdate = parseFloat(day.getDate()) < 10 ? '0' + day.getDate() : day.getDate()

       
        lists.push({ week: `星期${aa[day.getDay()]}`, day: `${(day.getMonth() + 1)}.${getdate}`, date: day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getdate })

        
      }
    }

    // console.log(lists)

    let num
    lists.forEach((val, index) => {
      let bb = oldday < 10 ? '0' + oldday : oldday
      let cc = oldmonth + '.' + oldday
      // console.log(cc)
      if (val.day == cc) {
        num = index
      }
    })
    // console.log(lists[num].date)
    
    this.setData({
      lists: lists,
      date: lists[num].date,
      dateIndex: num,
      toView: `a${num}`
    })

    this.getData()
    // console.log(lists)
    // console.log(this.data.toView)
  },
  telClick: function () {
    let phone = this.data.list.tel
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  tabClick: function (e) {
    let index = e.currentTarget.dataset.index

    this.setData({
      aaIndex: '0',
      bbIndex: '2',
      tabIndex: index,
      nextIndex: '',
    })

    // this.setData({
    //   tabIndex: index,
    //   nextIndex: '',

    // })
    if (index == '0') {
      this.getData()
    } else {
      this.datecount()
      this.getData2()
    }
  },
  dateClick: function (e) {
    let index = e.currentTarget.dataset.index
    let lists = this.data.lists
    let count = {
      count1: false,
      count2: false,
      start: '',
      end: '',
      start2: '',
      end2: '',
      money: '',
    }
    this.setData({
      dateIndex: index,
      date: lists[index].date,
      count: count,
      aaIndex: '0',
      bbIndex: '2',
    })

    if (this.data.tabIndex == '0') {
      this.getData()
    } else {
      this.datecount()
    }

  },
  getData: function () {
    let that = this
    // let bottomList = this.data.bottomList
    let id = this.data.list.id
    // console.log(this.data.list)
    let date = this.data.date
    let dateIndex = that.data.dateIndex
    let sortIndex = that.data.sortIndex
    let types = that.data.types
    let data
    if (types == '') {
      data = {
        storeId: id,
        date: date
      }
    } else {
      data = {
        type: types,
        storeId: id,
        date: date
      }
    }
    http.http({
      url: 'mp2/store/discountBooking',
      data: data,
      success(res) {
        let data2 = []
        for (let i in res.data.data.roomtypes) {
          data2.push(res.data.data.roomtypes[i])
        }
        that.setData({
          bottomList: res.data.data,
          bottomList2: data2,
          Room: data2[sortIndex]
        })
        console.log(res.data.data)
      }
    })
  },
  getData2: function () {
    let that = this
    let id = this.data.list.id
    // console.log(this.data.list)
    let dateIndex = that.data.dateIndex
    let sortIndex = that.data.sortIndex
    
    
    http.http({
      url: 'mp2/store/getroomtypebystore',
      data: {
        storeId: id,
      },
      success(res) {
        let data2 = []
        for (let i in res.data.data.roomtypes) {
          data2.push(res.data.data.roomtypes[i])
        }
        let date = new Date()
        let h = date.getHours()
        let Bom_date_Details = that.data.Bom_date_Details
        let Bom_date_Details2 = that.data.Bom_date_Details2
 
        if ((h) > 13) {

          Bom_date_Details.forEach((val, index) => {
            if (parseFloat(val) == (h)) {
              that.setData({
                'count.start': Bom_date_Details[index],
                'count.start2': Bom_date_Details2[index],
                'count.end': Bom_date_Details[index + 2],
                'count.end2': Bom_date_Details2[index + 2],
                'aaIndex': index,
                'bbIndex': '2'
              })
              // console.log(index)
              // console.log(val)
            }
          })

        } else {
          that.setData({
            'count.start': Bom_date_Details[0],
            'count.start2': Bom_date_Details2[0],
            'count.end': Bom_date_Details[2],
            'count.end2': Bom_date_Details2[2],
            'aaIndex': '0',
          })
        }
        console.log(that.data.count)
        that.setData({
          bottomList2: data2,
          Room: data2[sortIndex],
        })
        that.z_indexClick2()
      }
    })
  },
  addbbIndex: function () {
    let that = this
    let Bom_date_Details = that.data.Bom_date_Details
    let Bom_date_Details2 = that.data.Bom_date_Details2
    let index1 = this.data.aaIndex

    let index2 = this.data.bbIndex
    index2++

    if (index2 < Bom_date_Details.length - index1) {
      let index = parseFloat(index1) + parseFloat(index2)
      // console.log(index)
      // console.log(Bom_date_Details[index])
      that.setData({
        'count.end': Bom_date_Details[index],
        'count.end2': Bom_date_Details2[index],
        bbIndex: index2
      })
    }
    this.z_indexClick2()
    // console.log(index2)
    // console.log(Bom_date_Details)
  },
  deletebbIndex: function () {
    let that = this
    let Bom_date_Details = that.data.Bom_date_Details
    let Bom_date_Details2 = that.data.Bom_date_Details2
    let index1 = this.data.aaIndex

    let index2 = this.data.bbIndex
    index2--
    if (index2 > 1) {
      let index = parseFloat(index1) + parseFloat(index2)
      that.setData({
        'count.end': Bom_date_Details[index],
        'count.end2': Bom_date_Details2[index],
        bbIndex: index2
      })
    }
    this.z_indexClick2()
  },
  afternoonClick: function () {
    // console.log(this.data.Room)
    this.setData({
      DiscountShow: true,
      z_index: true,
      nextIndex: '0'
    })
    let Bom_date_Details = ['13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30',]

    let date = this.data.date
    
    let Bom_date_Details2 = []

    Bom_date_Details.forEach((val, index) => {
      Bom_date_Details2.push(date + ' ' + val)
    })

    let bottomList = this.data.bottomList
    this.setData({
      'RoomDate.money': bottomList.afternoon.price,
      'RoomDate.date': '13:00-18:00',
      Bom_date_Details: Bom_date_Details,
      Bom_date_Details2: Bom_date_Details2,
      Bom_date_Detail: Bom_date_Details2[0],
      Bom_date_Detail2: Bom_date_Details2[Bom_date_Details2.length - 1],
      Bom_date_DetailsEnd: '18:00',
      dates2: (Bom_date_Details2[Bom_date_Details2.length - 1]).slice(0, 10)
    })
    // console.log(Bom_date_Details2[0])
  },
  goldClick: function () {
    // console.log(this.data.Room)
    this.setData({
      DiscountShow: true,
      z_index: true,
      nextIndex: '1'
    })

    let Bom_date_Details = ['18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '00:00', '01:00',]

    let date = this.data.date
    let day = new Date(date)
    day.setDate(day.getDate() + 1)
    let getDate = (day.getDate() < 10 ? '0' + day.getDate() : day.getDate())
    let date2 = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getDate

    let Bom_date_Details2 = []


    Bom_date_Details.forEach((val, index) => {

      if (index > 5) {
        Bom_date_Details2.push(date2 + ' ' + val)
      } else {
        Bom_date_Details2.push(date + ' ' + val)
      }
    })

    let bottomList = this.data.bottomList
    this.setData({
      'RoomDate.money': bottomList.afternoon.price,
      'RoomDate.date': '18:00-02:00',
      Bom_date_Details: Bom_date_Details,
      Bom_date_Details2: Bom_date_Details2,
      Bom_date_Detail: Bom_date_Details2[0],
      Bom_date_Detail2: Bom_date_Details2[Bom_date_Details2.length - 1],
      Bom_date_DetailsEnd: '02:00',
      dates2: (Bom_date_Details2[Bom_date_Details2.length - 1]).slice(0,10)
    })
    // console.log(Bom_date_Details2)
  },
  nightClick: function () {

    this.setData({
      DiscountShow: true,
      z_index: true,
      nextIndex: '2'
    })

    let Bom_date_Details = ['01:00', '01:30', '02:00', '03:30', '04:00', '04:30', '05:00', '05:30',]

    let date = this.data.date
    let day = new Date(date)
    day.setDate(day.getDate() + 1)
    let getDate = (day.getDate() < 10 ? '0' + day.getDate() : day.getDate())
    let date2 = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getDate
    
    let Bom_date_Details2 = []
    Bom_date_Details.forEach((val, index) => {
      Bom_date_Details2.push(date2 + ' ' + val)
    })
   
    let bottomList = this.data.bottomList
    this.setData({
      'RoomDate.money': bottomList.afternoon.price,
      'RoomDate.date': '01:00-06:00',
      Bom_date_Details: Bom_date_Details,
      Bom_date_Details2: Bom_date_Details2,

      Bom_date_Detail: Bom_date_Details2[0],
      Bom_date_Detail2: Bom_date_Details2[Bom_date_Details2.length - 1],
      Bom_date_DetailsEnd: '06:00',
      dates2: (Bom_date_Details2[Bom_date_Details2.length - 1]).slice(0, 10)
    })
    
  },
  //选择弹出框的时间
  index_click: function (e) {

    let index = e.currentTarget.dataset.index
    let Bom_date_Details = this.data.Bom_date_Details
    let Bom_date_Details2 = this.data.Bom_date_Details2
    
    if (this.data.tabIndex == '0') {
      this.setData({
        aaIndex: index,
        Bom_date_Detail: Bom_date_Details2[index],
        Bom_date_Detail2: Bom_date_Details2[Bom_date_Details2.length - 1]
      })
    } else {
      let date = new Date()
      let h = date.getHours()
      let newindex
      Bom_date_Details.forEach((val, index) => {
        if (parseFloat(val) == (h + 1)) {
          newindex = index
        }
      })
      if (index < newindex) {
        wx.showModal({
          content: '当前时间已过',
        })
      } else {
        if ((Bom_date_Details.length - index)<3){
          index = Bom_date_Details.length - 3
        }
        // console.log(Bom_date_Details.length)
        // console.log(index)
        this.setData({
          aaIndex: index,
          'count.start': Bom_date_Details[index],
          'count.start2': Bom_date_Details2[index],
          'bbIndex': '2',
          'count.end': Bom_date_Details[index + 2],
          'count.end2': Bom_date_Details2[index + 2],
        })
      }

    }

    // console.log(Bom_date_Details2)

  },
  datecount: function (e) {
    // let index = '1'
    // let index = e.currentTarget.dataset.index || '1'
    if (e) {
      this.setData({
        DiscountShow: true,
        z_index: true
      })
    }

    let Bom_date_Details = ['13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00']

    let date = this.data.date
    let day = new Date(date)
    day.setDate(day.getDate() + 1)
    let getDate = (day.getDate() < 10 ? '0' + day.getDate() : day.getDate())
    let date2 = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + getDate

    let Bom_date_Details2 = []
    Bom_date_Details.forEach((val, index) => {
      if (index > 10) {
        Bom_date_Details2.push(date2 + ' ' + val)
      } else {
        Bom_date_Details2.push(date + ' ' + val)
      }
    })
    this.setData({
      'count.count1': true,
      'count.count2': false,
    })
    
    let bottomList = this.data.bottomList
    this.setData({
      'RoomDate.money': '',
      'RoomDate.date': '09:00-06:00',
      Bom_date_Details: Bom_date_Details,
      Bom_date_Details2: Bom_date_Details2,
    })
    this.getData2()
  },
  next: function () {
    let date = this.data.dates2

    if (this.data.RoomDate.money > 0) {
      let id2 = {
        // Bom_date_Detail2: this.data.Bom_date_Detail2,
        Bom_date_Detail2: date + ' ' + this.data.Bom_date_DetailsEnd,

        Bom_date_Detail: this.data.Bom_date_Detail,
        Room: this.data.Room,
        list: this.data.list,
        index: this.data.tabIndex,
        money: this.data.RoomDate.money,
        nextIndex: this.data.nextIndex
      }
      console.log(id2)
      let id = JSON.stringify(id2)

      if (wx.getStorageSync('tel')) {
        wx.navigateTo({
          url: `/pages/order/addOrderInfo/index?id=${id}`,
        })
      } else {
        http.goUrl({
          url: `../login/my_login/index?id=${id}`
        })
      }
    } else {
      wx.showModal({
        content: '请正确选择',
      })
    }
  },
  OK: function () {
    if (this.data.count.money > 0) {
      let id2 = {
        Bom_date_Detail2: this.data.count.end2,
        Bom_date_Detail: this.data.count.start2,
        Room: this.data.Room,
        list: this.data.list,
        index: this.data.tabIndex,
        money: this.data.count.money,
        nextIndex: this.data.nextIndex
      }
      // console.log(id2)

      let id = JSON.stringify(id2)

      if (wx.getStorageSync('tel')) {
        wx.navigateTo({
          url: `/pages/order/addOrderInfo/index?id=${id}`,
        })
      } else {
        http.goUrl({
          url: `../login/my_login/index?id=${id}`
        })
      }
    } else {
      wx.showModal({
        content: '请正确选择',
      })
    }
  },
  z_indexClick2: function () {
    let that = this
    // this.setData({
    //   DiscountShow: false,
    //   z_index: false
    // })
    let count = this.data.count
    let data = this.data
    // console.log(count)
    let datas = {
      starttime: count.start2,
      endtime: count.end2,
      storeid: data.list.id,
      type: data.Room.type,
    }
    http.http({
      url: 'mp2/store/timebooking',
      data: datas,
      success(res) {
        that.setData({
          'count.money': res.data.data.price.toFixed(2)
        })
      }
    })
  },
  //导航
  maps: function () {
    let latitude = parseFloat(wx.getStorageSync('latitude'))
    let longitude = parseFloat(wx.getStorageSync('longitude'))
    let that = this

    wx.openLocation({
      longitude: latitude,
      latitude: longitude,
      address: that.data.list.address,
      success(res) {
        console.log(res, latitude, longitude)
      },
      fail(res) {
        console.log(res)
      },
    })
  },

  //点击包厢
  BomListClick: function (e) {
    let index = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    this.setData({
      sortIndex: index,
      types: type
    })
    this.getData()
  },
  Z_index: function () {
    this.setData({
      DiscountShow: false,
      z_index: false
    })
  },
  z_indexClick: function () {
    this.setData({
      DiscountShow: false,
      z_index: false
    })
  },
  onLoad: function (options) {

    let list = JSON.parse(options.list)
    this.setData({
      list: list
    })
    this.date()

    // console.log(list)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})